//
//  WmcBubbleVM.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/11.
//

import UIKit

public extension WmcBubbleVM {
    /// 方向
    enum Direction {
        case top
        case left
        case bottom
        case right
    }

    /// 关闭类型
    enum CloseType {
        /// 手动关闭
        case close
        /// 自动关闭
        case autoDismiss(_ seconds: CGFloat)
    }

    /// 关闭样式
    enum CloseStyle {
        /// 线性图片
        case line
        /// 填充图片
        case fill
        /// 圆型填充图片
        case roundFill
        /// 自定义图片
        case customImage(_ image: UIImage)
    }

    /// 内容样式
    enum ContentStyle {
        /// 图文样式
        public enum ImageTextStyle {
            /// 图上文下
            case imageTopTextBottom
            /// 图左文右
            case imageLeftTextRight
            /// 图下文上
            case imageBottomTextTop
            /// 图右文左
            case imageRightTextLeft
        }

        /// 图片配置
        public struct ImageConfig {
            // Public

            /// 默认配置
            public static var defaultConfig: ImageConfig {
                return .init()
            }

            /// 图片大小
            public var size: CGSize?

            /// 内容模式
            public var contentMode: UIView.ContentMode = .scaleAspectFit
            /// 背景颜色
            public var backgroundColor: UIColor = .clear

            /// 圆角
            public var cornerRadius: CGFloat = 0
            /// 裁切
            public var masksToBounds: Bool = true
            /// 阴影颜色
            public var shadowColor: UIColor?
            /// 阴影透明
            public var shadowOpacity: CGFloat = 0
            /// 阴影半径
            public var shadowRadius: CGFloat = 0
            /// 阴影偏移
            public var shadowOffset: CGSize = .zero

            // Internal

            // 将设置赋值给视图
            func settingImageView(_ imageView: UIImageView) {
                imageView.contentMode = contentMode
                imageView.backgroundColor = backgroundColor
                imageView.layer.cornerRadius = cornerRadius
                imageView.layer.masksToBounds = masksToBounds
                imageView.layer.shadowColor = shadowColor?.cgColor
                imageView.layer.shadowOpacity = shadowOpacity.yp.float
                imageView.layer.shadowRadius = shadowRadius
                imageView.layer.shadowOffset = shadowOffset
            }
        }

        /// 文字配置
        public struct TextConfig {
            // Public

            /// 默认配置
            public static var defaultConfig: TextConfig {
                return .init()
            }

            /// 文字颜色
            public var textColor: UIColor = .white
            /// 文字字体
            public var font: UIFont?
            /// 对齐模式
            public var textAlignment: NSTextAlignment = .center
            /// 换行模式
            public var lineBreakMode: NSLineBreakMode = .byWordWrapping
            /// 字号根据宽度自适应
            public var adjustsFontSizeToFitWidth: Bool = false

            /// 内容模式
            public var contentMode: UIView.ContentMode = .scaleAspectFit
            /// 背景颜色
            public var backgroundColor: UIColor = .clear

            /// 圆角
            public var cornerRadius: CGFloat = 0
            /// 裁切
            public var masksToBounds: Bool = true

            /// 阴影颜色
            public var shadowColor: UIColor?
            /// 阴影透明
            public var shadowOpacity: CGFloat = 0
            /// 阴影半径
            public var shadowRadius: CGFloat = 0
            /// 阴影偏移
            public var shadowOffset: CGSize = .zero

            // Internal

            // 将设置赋值给视图
            func settingLabel(_ label: UILabel) {
                label.textColor = textColor
                label.font = font
                label.textAlignment = textAlignment
                label.lineBreakMode = lineBreakMode
                label.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth
                label.contentMode = contentMode
                label.backgroundColor = backgroundColor
                label.layer.cornerRadius = cornerRadius
                label.layer.shadowColor = shadowColor?.cgColor
                label.layer.shadowOpacity = shadowOpacity.yp.float
                label.layer.shadowRadius = shadowRadius
                label.layer.shadowOffset = shadowOffset
            }
        }

        /// 图文属性配置
        public struct ImageTextConfig {
            // Public

            /// 默认配置
            public static var defaultConfig: ImageTextConfig {
                return .init()
            }

            /// 图片配置
            public var imageConfig: ImageConfig = .defaultConfig
            /// 文字配置
            public var textConfig: TextConfig = .defaultConfig
            /// 图文间距
            public var imageTextSpacing: CGFloat = 8

            // Internal

            // 将设置赋值给视图
            func settingView(_ imageView: UIImageView, label: UILabel) {
                imageConfig.settingImageView(imageView)
//                textConfig.settingLabel(label)
            }
        }

        /// 纯文本
        case text(_ text: String,
                  config: TextConfig = .defaultConfig)
        /// 纯图片
        case image(_ image: UIImage,
                   config: ImageConfig = .defaultConfig)
        /// 图文
        case imageText(image: UIImage,
                       string: String,
                       style: ImageTextStyle,
                       config: ImageTextConfig = .defaultConfig)
        /// 纯富文本
        case attributedText(_ attributedText: NSAttributedString,
                            config: TextConfig = .defaultConfig)
        /// 图片富文本
        case imageAttributedText(image: UIImage,
                                 attributedText: NSAttributedString,
                                 style: ImageTextStyle,
                                 config: ImageTextConfig = .defaultConfig)
        /// 自定义视图
        case custom(view: UIView)
    }

    /// 箭头样式
//    enum ArrowSharpStyle {
//        /// 锋利的
//        case sharp
//        /// 圆角，传入圆半径
//        case round(cornerRadius: CGFloat)
//    }
}

// MARK: - [WmcBubbleVM]

open class WmcBubbleVM: WmcBaseVM {
    /// 展示方向
    open var showDirection: Direction = .top
    /// 展示添加到哪个View上
    /// 如果是外部调用，则无需处理此参数
    var showInView: UIView?
    
    /// 标注在哪个View
    /// 如果是外部调用，则无需处理此参数
    var markView: UIView?
    /// 箭头顶端距离标记视图的距离
    open var markViewSpacing: CGFloat = 8
    /// 中心距离标记视图中心的偏移量，上下为centerX，左右为centerY
    open var markViewCenterOffset: CGFloat = 0
    
    /// 关闭按钮的位置
    open var closeBtnDirection: Direction = .right
    /// 关闭按钮距离内容视图的距离
    open var closeBtnContentViewSpacing: CGFloat = 8
    /// 关闭类型
    open var closeType: CloseType = .close
    /// 关闭样式
    open var closeStyle: CloseStyle = .line
    /// 关闭按钮颜色
    open var closeBtnTintColor: UIColor = .white
    /// 是否使用手动关闭按钮
    open var useCloseBtn: Bool = false

    /// 内容样式
    open var contentStyle: ContentStyle = .text("这里显示内容")
    /// 内容内间距
    open var contentInsetEdge: UIEdgeInsets = 8.yp.uiEdge
    /// 内容颜色
    open var contentBackgroundColor: UIColor = .wmc_mainTheme
    /// 内容圆角
    open var contentCornerRadius: CGFloat = 8

    /// 气泡显示时的大小，不包含箭头部分
    open var bubbleSize: CGSize = .init(width: 120, height: 35)
    /// 气泡显示时，距离window‘的内间距
    open var bubbleShowWindowEdge: UIEdgeInsets = Wmc_standardEdge
    /// 气泡阴影颜色
    open var bubbleShadowColor: UIColor = .wmc_mainTheme
    /// 气泡阴影透明
    open var bubbleShadowOpacity: CGFloat = 0.3
    /// 气泡阴影半径
    open var bubbleShadowRadius: CGFloat = 8
    /// 气泡阴影偏移
    open var bubbleShadowOffset: CGSize = .zero

    /// 气泡箭头高度
    open var arrowHeight: CGFloat = 8
    /// 气泡箭头宽度（决定箭头开合角大小）
    open var arrowWidth: CGFloat = 16
    /// 气泡箭头偏移量，上下为centerX，左右为centerY
    open var arrowOffset: CGFloat = 0
    /// 气泡箭头尖角样式
//    open var bubbleArrowSharpStyle: ArrowSharpStyle = .sharp
}
